import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class RotateImageLabel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7516178276818971031L;
	private double rotateAngle = 0.0;
	private double wheel1Rotate = 0.0;
	private double wheel2Rotate = 0.0;
	private Image wheel;
	private int wheel_w, wheel_h;
	private Image image;
	private int image_w;
	private int image_h;
	AtomicInteger wheelSpeed[];
	
	public RotateImageLabel(ImageIcon icon, AtomicInteger[] wheelSpeed) {
		this.image = icon.getImage();
		this.wheelSpeed = wheelSpeed;
		image_w = image.getWidth(this);
		image_h = image.getHeight(this);
		wheel = new ImageIcon("resources/wheel.png").getImage();
		wheel_w = wheel.getHeight(this);
		wheel_h = wheel.getHeight(this);
		
		
	}
	
	public void calculatePosition(){
		int step = 2;
		double a = 0.0;
		if(wheelSpeed[0].get() > 0 && wheelSpeed[1].get() == 0){
			a = step/100.0;
		} else if(wheelSpeed[0].get() == 0 && wheelSpeed[1].get() > 0) {
			a = -step/100.0;
		}
		if(wheelSpeed[0].get() > 0){
			wheel1Rotate += 2.5;
			wheel1Rotate %= 360;
		}
		if(wheelSpeed[1].get() > 0){
			wheel2Rotate += 2.5;
			wheel2Rotate %= 360;
		}
		rotateAngle += a;
		repaint();
	}
	
	@Override
	protected void paintComponent(Graphics g){
		
		Graphics2D gr = (Graphics2D) g;
		
		Rectangle r =  this.getBounds();
		gr.setColor(Color.WHITE);
		gr.fillRect(0, 0, getWidth(), getHeight());
		AffineTransform af = new AffineTransform();
		af.setToTranslation((r.width-image_w)/2, (r.height-image_h)/2);
		af.rotate(rotateAngle, image_w/2, image_h/2);
		gr.drawImage(image, af, this);
		
		
		af.setToTranslation(10, (r.height-wheel_h)/2);
		af.rotate(Math.toRadians(wheel1Rotate), wheel_w/2, wheel_h/2);
		
		gr.drawImage(wheel, af, this);
		
		af.setToTranslation(r.width-wheel_w - 10, (r.height-wheel_h)/2);
		af.rotate(Math.toRadians(wheel2Rotate), wheel_w/2, wheel_h/2);
		
		gr.drawImage(wheel, af, this);
		
	}
}
